package com.jic.market.impl.rest.discountamount.processor;

import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.common.util.Money;
import com.jic.market.entity.PatchGrouponStockLmtEntity;
import com.jic.market.impl.rest.discountamount.model.PromotionAmountModel;
import com.jic.market.impl.rest.discountamount.po.GoodsPo;
import com.jic.market.mapper.PatchGrouponStockLmtEntityMapper;
import com.jic.market.mapper.PatchGrouponThemeEntityMapper;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.request.rest.discountamount.DiscountAmountRefundRequest;
import com.jic.market.request.rest.discountamount.GrouponDiscountAmountOrderCreateRequest;
import com.jic.market.request.rest.groupon.CancelPayGroupInstDetailRequest;
import com.jic.market.request.rest.groupon.LaunchOrJoinGrouponRequest;
import com.jic.market.response.rest.groupon.LaunchOrJoinGrouponResponse;
import com.jic.market.response.rest.groupon.PostPayGroupInstDetailResponse;
import com.jic.market.service.rest.groupon.GrouponRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 拼团
 * @author yinhaijun
 * @date: 2020/4/7
 */
@Service("GrouponDiscountAmountProcessor")
public class GrouponDiscountAmountProcessor extends AmountProcessor {

    @Autowired
    private PromotionRuleEntityMapper promotionRuleEntityMapper;

    @Autowired
    private PatchGrouponStockLmtEntityMapper patchGrouponStockLmtEntityMapper;

    @Autowired
    private PatchGrouponThemeEntityMapper patchGrouponThemeEntityMapper;

    @Autowired
    private GrouponRestService grouponRestService;

    @Override
    public Boolean isCondition(PromotionAmountModel promotionAmountModel, GoodsPo goodsPo, String thirdUserId) {
        if(!checkCommonLimitRule(promotionAmountModel.getPromotionId(), goodsPo.getSkuId(), goodsPo.getNum(), thirdUserId)){
            return false;
        }

        return true;
    }

    @Override
    public BigDecimal promotionDiscountTotalAmount(PromotionAmountModel promotionAmountModel, GoodsPo goodsPo, Object request) {
        PatchGrouponStockLmtEntity patchGrouponStockLmtEntity = patchGrouponStockLmtEntityMapper.getEntityByPromotionIdAndMpId(Long.parseLong(promotionAmountModel.getPromotionId()), goodsPo.getSkuId());
        if(null == patchGrouponStockLmtEntity){
            throw new MarketException(MarketErrorCodeEnum.PARAM_ERROR, "找不到对应的拼团商品" + goodsPo.getThirdSkuId());
        }
        // 获得团购价
        BigDecimal grouponPrice = patchGrouponStockLmtEntity.getGrouponPrice();
        // 计算优惠总价 = (原价-团购价)*数量
        BigDecimal promotionDiscountSingleAmount = Money.subtract(goodsPo.getMarketPrice(), grouponPrice);
        BigDecimal promotionDiscountTotalAmount = Money.multiply(promotionDiscountSingleAmount, new BigDecimal(goodsPo.getNum()));
        return promotionDiscountTotalAmount;
    }

    @Override
    public void createAfter(PromotionAmountModel promotionAmountModel, Object request){
        GrouponDiscountAmountOrderCreateRequest grouponRequest = (GrouponDiscountAmountOrderCreateRequest) request;
        // 调用参团活动
        LaunchOrJoinGrouponRequest launchOrJoinGrouponRequest = new LaunchOrJoinGrouponRequest();
        launchOrJoinGrouponRequest.setGroupCode(grouponRequest.getPromotionCode());
        launchOrJoinGrouponRequest.setThirdProductCode(grouponRequest.getPromotionGoodsList().get(0).getThirdProductCode());
        launchOrJoinGrouponRequest.setThirdOrderCode(grouponRequest.getOrderNo());
        launchOrJoinGrouponRequest.setBuyNum(Long.valueOf(grouponRequest.getPromotionGoodsList().get(0).getNum()));
        launchOrJoinGrouponRequest.setGrouponInstCode(grouponRequest.getGrouponInstCode());
        launchOrJoinGrouponRequest.setLaunchFlag(grouponRequest.getLaunchFlag());
        launchOrJoinGrouponRequest.setThirdUserId(grouponRequest.getThirdUserId());
        LaunchOrJoinGrouponResponse launchOrJoinGrouponResponse = grouponRestService.launchOrJoinGroupon(launchOrJoinGrouponRequest);
        promotionAmountModel.setRuleData(launchOrJoinGrouponResponse);
    }

    @Override
    public void refundAfter(PromotionAmountModel promotionAmountModel, Object request){
        DiscountAmountRefundRequest discountAmountRefundRequest = (DiscountAmountRefundRequest) request;
        CancelPayGroupInstDetailRequest cancelPayGroupInstDetailRequest = new CancelPayGroupInstDetailRequest();
        cancelPayGroupInstDetailRequest.setThirdUserId(discountAmountRefundRequest.getThirdUserId());
        cancelPayGroupInstDetailRequest.setThirdOrderCode(discountAmountRefundRequest.getOrderNo());
        PostPayGroupInstDetailResponse launchOrJoinGrouponResponse = grouponRestService.cancelPayGroupInstDetail(cancelPayGroupInstDetailRequest);
        promotionAmountModel.setRuleData(launchOrJoinGrouponResponse);
    }

}
